// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NVRAM_H_
#define NVRAM_H_

#include <stdint.h>
#include <cstring>
#include <vector>

namespace tpmd {

class Nvram {
 public:
  typedef std::vector<uint8_t> Bytes;

  enum {
    // If this flag is supplied to Allocate(), this region will be locked once
    // written.
    NVRAM_LOCKONCE = 0x00000001,
  };

  // Drops this reference to the backing Nvram slot. Note that this does _not_
  // deallocate the underlying Nvram slot, but it _does_ destroy this object's
  // reference to it. If you need to deallocate the slot, use Free() below.
  virtual ~Nvram() { }

  // Allocates a new Nvram slot and makes this object a reference to it. Returns
  // true for success.
  virtual bool Allocate(uint32_t slot, size_t len, uint32_t flags) = 0;

  // Frees the underlying Nvram slot. Does not delete this object, but destroys
  // the reference to the slot. Returns true for success, in which case the
  // reference is destroyed; returns false for failure, in which case the
  // reference is intact.
  virtual bool Free(uint32_t slot) = 0;

  // Writes the supplied bytes to this Nvram slot. The length of |bytes| must be
  // the same as the size of this Nvram slot. Returns true for success.
  virtual bool Write(uint32_t slot, const Bytes& bytes) = 0;

  // Reads the contents of this Nvram slot. Returns true for success, in which
  // case |bytes| is modified; returns false for failure, in which case |bytes|
  // is untouched.
  virtual bool Read(uint32_t slot, Bytes* bytes) = 0;

  // Returns whether this region has ever been written.
  virtual bool is_defined(uint32_t slot) = 0;

  // Returns whether this region is locked, preventing writing.
  virtual bool is_locked(uint32_t slot) = 0;

  // Returns the size of this region, in bytes.
  virtual size_t size(uint32_t slot) = 0;

  // Returns how many slots are available.
  virtual size_t num_slots() = 0;
};

}  // namespace tpmd

#endif /* !NVRAM_H_ */
